//
//  JYUserAccount.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/3.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit

class JYUserAccount: NSObject, NSCoding {
    // MARK:- 属性
    ///用户ID
    var uid : String?
    ///授权accessToken
    var access_token : String?
    ///过期时间
    var expires_in :NSTimeInterval = 0.0 {
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    ///过期日期
    var expires_date : NSDate?
    
    ///用户昵称
    var name : String?
    ///用户头像URL
    var avatar_large :String?
    
    
    
    
    // MARK:- 自定义构造方法
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    // MARK:- 重写description属性
    override var description: String {
        return dictionaryWithValuesForKeys(["access_token", "expires_date", "uid", "name", "avatar_large"]).description
    }
    
    // MARK:- 归档&解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        uid = aDecoder.decodeObjectForKey("uid") as? String
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}


